import jsonschema


class JsonSchemaUtil(object):

   @classmethod
   def json_schema(cls, json_data, expect_data):
       json_schema = {
           "$schema": "https://json-schema.org/draft/2020-12/schema",
           "$id": "https://example.com/product.schema.json",
           "title": "iHRM断言",
           "description": "响应体的数据校验",
           "type": "object",
           "properties":
               {
                   "success": {
                       "description":'操作成功标记',
                       "type": "boolean",
                       "const": expect_data.get('success')
                   },
                   "code": {
                       "description": '错误码',
                       "type": "number",
                       "const": expect_data.get('code')
                   },
                   "message": {
                       "description": '消息',
                       "type": "string",
                       "pattern": expect_data.get('message')
                   },
                   "data": {
                       "description": '响应返回的数据，失败返回空'
                   }
               },
           "required": ["success", "code", "message"]
       }

       # 验证：
       jsonschema.validate(instance=json_data, schema=json_schema)